// pages/bookEnter/record/index.js
const appData = getApp()
Component({
  /**
   * 组件的属性列表
   */
  lifetimes: {
    attached() {
      this.getRecordData() // 获取gender初始数据
    },
    // 在组件实例进入页面节点树时执行
    detached() {
      // 在组件实例被从页面节点树移除时执行
    }
  },
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    record: {},
    index: "male", // 用来控制初始显示哪个数据
    style: ["#fff", "", "", ""], // 用来控制Left的样式、
  },

  /**
   * 组件的方法列表
   */
  methods: {
    getRecordData() {
      let record = appData.globalData.record;
      delete record.ok;
      if (record.male) {
        for (let key in record) {
          record[key].forEach(val => {
            for (let i = 0; i < val.bookCover.length; i++) {
              let num = val.bookCover[i].indexOf("http");
              val.bookCover[i] = decodeURIComponent(val.bookCover[i].slice(num));
            }
          })
        }
        this.setData({
          record: record
        })

        // this.saveAppData()
      } else {
        setTimeout(() => {
          this.getRecordData()
        }, 300)
      }
    },


    toggle(e) {
      let id = e.target.id
      this.setData({
        index: id
      })
      let num = 0;
      if (id === "male") {
        num = 0;
      } else if (id === "female") {
        num = 1;
      } else if (id === "press") {
        num = 2;
      } else {
        num = 3;
      }

      let style = this.data.style;
      style = style.map((val, index) => {
        if (index === num) {
          val = "#fff"
        } else {
          val = ""
        }
        return val;
      })
      this.setData({
        style
      })
    },


    toerRecord(e) { // 跳转
      let index = this.data.index;
      let id = e.currentTarget.id
      wx.navigateTo({
        url: `/pages/bookEnter/record/erRecord/index?id=${id}&index=${index}`,
      })
    },

  }
})